package com.liuhung.base.util;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
 * 汉字转拼音
 * @author Administrator
 *
 */
public class Chinese2Pinyin {
	/**
	 * 将汉字转换成拼音
	 * @param str
	 * @return
	 */
	public static String toPinYin(String str) {
		String py = "";
		String[] t = new String[str.length()];
		
		char [] hanzi=new char[str.length()];
        for(int i=0;i<str.length();i++){
            hanzi[i]=str.charAt(i);
        }
		
		net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t1 = new HanyuPinyinOutputFormat();
        t1.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t1.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t1.setVCharType(HanyuPinyinVCharType.WITH_V);
        
		try {
			for (int i = 0; i < str.length(); i++) {
				if ((str.charAt(i) >= 'a' && str.charAt(i) < 'z')
						|| (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
						|| (str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
					py += str.charAt(i);
				} else {
			            t = PinyinHelper.toHanyuPinyinStringArray(hanzi[i], t1);
			            py=py+t[0];
			        }
				}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}

		return py.trim().toString();
	}
	
	 public static void main(String args[]){  
	        System.out.println(Chinese2Pinyin.toPinYin("汉字2hello转拼音"));  
	    }
}
